<?php
use Mirage\Base\DB;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of TaskModel
 *
 * @author bry
 */
class TaskModel extends \Mirage\Abstraction\AbstractModel{
    
    public $user_id;
    public $task;
    public $status;
    public $problem;
    public $solution;
    public $remarks;
    public $date;
    
    public function __construct() {
        $this->db = DB::app('report');
    }
    
    /**
     *
     * @var TaskModel
     */
    private static $_instance;
    
//    /**
//     * Get instance of TaskModel
//     * @return TaskModel 
//     */
//    public static function app() {
//        if(self::$_instance == null)
//            self::$_instance = new TaskModel();
//        return self::$_instance;
//    }
    
    /**
     * 
     * @param type $class
     * @return TaskModel
     */
    public static function model($class=__CLASS__) {
        if(!isset(self::$_instances[$class])) {
            self::$_instances[$class] = new $class();
        }
        return self::$_instances[$class];
    }
    
    protected function rules() {
        return array(
            array('fields'=>array(),'validator'=>'','options'=>array()),
        );
    }
    
    public function findAll() {
        if(isset($_GET['rows']))
            $limit = $_GET['rows'];
        
        
        $sql = 'SELECT t.id, t.date, t.task, t.status, t.problem, t.solution, t.remarks from task t ' . 
                'INNER JOIN users u ON t.user_id = u.id WHERE u.id = :id';
        $this->db->exec($sql,array(':id'=>User::getInstance()->getID()));
        $data = $this->db->findAll();
        $this->db->exec('SELECT COUNT(*) totalrow FROM task WHERE id = :id',array(':id'=>User::getInstance()->getID()));
        $row = $this->db->find();
        if(!isset ($row['totalrow']))
            $row['totalrow'] = 0;
        return JqGrid::getJSON($row['totalrow'], $data,'',true);
    }
    
//    public function findAll() {
//        $limit = $_GET['rows'];
//        $sql = 'SELECT m.id, m.movie_title FROM movies m ' . 
//            'INNER JOIN movie_types mt ON mt.movie_id = m.id ' . 
//            'INNER JOIN ref_types rt ON mt.type_id = rt.id ' . 
//            'GROUP BY m.id';
//        
//        $this->db->exec($sql);
//        $data = $this->db->findAll();
//        $this->db->exec('SELECT COUNT(*) totalrow FROM movies');
//        $row=$this->db->find();
//        if(!isset ($row['totalrow']))
//            $row['totalrow'] = 0;
//        return JqGrid::getJSON($row['totalrow'], $data,'',true);
//    }
    
    public function findByID($id) {
        $sql = 'SELECT * FROM task WHERE id = :id';
        $this->db->exec($sql,array(':id'=>$id));
        return $this->db->find();
    }
    
    public function deleteByID($id) {
        $sql = 'DELETE FROM task WHERE id = :id';
        return $this->db->exec($sql, array(':id'=>$id));
    }
    
    public function save() {
        $sql = 'INSERT INTO task (date,task,status,problem,solution,remarks) VALUES(:date,:task,:status,:problem,:solution,:remarks)';
        $this->db->exec($sql, array(
            ':date'=>  $this->date,
            ':task'=>$this->task,
            ':status'=>$this->status,
            ':problem'=>$this->problem,
            ':solution'=>  $this->solution,
            ':remarks'=>  $this->remarks));
        $this->id = $this->db->getPdo()->lastInsertId();
    }
    
    public function updateByID($id) {
        $sql = 'UPDATE task SET date = :date, task = :task, status = :status, problem = :problem, solution = :solution, remarks = :remarks ' . 
            'WHERE id = :id';
        
        return $this->db->exec($sql, array(
                ':id'=>$id,
                ':date'=>  $this->date,
                ':task'=>$this->task,
                ':status'=>$this->status,
                ':problem'=>$this->problem,
                ':solution'=>  $this->solution,
                ':remarks'=>  $this->remarks));
    }
    
}

?>
